<template>
  <div class="login-container">
    <div class="login-wrapper">
      <div class="login-header">
        <h2>欢迎登录</h2>
        <p>请输入您的账号信息</p>
      </div>

      <el-form :model="form" :rules="rules" label-width="80px" ref="formRef" class="login-form">
        <el-form-item label="用户名" prop="username" class="form-item">
          <el-input v-model="form.username" placeholder="请输入用户名" class="input-field" />
        </el-form-item>

        <el-form-item label="密码" prop="password" class="form-item">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
            class="input-field"
          />
        </el-form-item>

        <el-form-item label="验证码" prop="captcha_code" class="form-item">
          <div class="captcha-wrapper">
            <el-input
              v-model="form.captcha_code"
              type="text"
              placeholder="请输入验证码"
              class="input-field captcha-input"
            />
            <div class="captcha-image-container" @click="refreshCaptcha">
              <img v-if="captchaImg.src" :src="captchaImg.src" alt="验证码" class="captcha-image" />
              <div v-else class="captcha-placeholder">加载中...</div>
            </div>
          </div>
        </el-form-item>

        <el-form-item class="form-item">
          <div class="form-options">
            <el-checkbox v-model="form.remember" class="remember-checkbox">记住密码</el-checkbox>
            <el-link type="primary" :underline="false" class="forgot-link">忘记密码？</el-link>
          </div>
        </el-form-item>

        <el-form-item class="form-item">
          <el-button
            type="primary"
            class="login-button"
            :loading="loading"
            @click="submitForm(formRef)"
          >
            {{ loading ? '登录中...' : '登录' }}
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { loginApi, getcaptcha } from '@/API/AllApi'
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const formRef = ref()
const form = ref({
  username: '',
  password: '',
  captcha_code: '',
  captcha_id: '',
  remember: false,
})
const loading = ref(false)

const captchaImg = ref({
  src: '',
  id: '',
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 15, message: '长度在3到25个字符之间', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' },
  ],
  captcha_code: [
    { required: true, message: '请输入验证码', trigger: 'blur' },
    { min: 4, max: 6, message: '验证码为6位', trigger: 'blur' },
  ],
}

const submitForm = (formEl) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      loginApi({
        username: form.value.username,
        password: form.value.password,
        captcha_id: captchaImg.value.id,
        captcha_code: form.value.captcha_code,
      })
        .then((res) => {
          if (res && res.data) {
            localStorage.setItem('login', JSON.stringify(res.data))
            router.push('/journalism')
          }
        })
        .catch((err) => {
          console.error('登录请求失败:', err)
          refreshCaptcha()
        })
    }
  })
}

const refreshCaptcha = () => {
  getcaptcha()
    .then((res) => {
      if (res && res.data && res.data.image) {
        captchaImg.value.src = res.data.image
        captchaImg.value.id = res.data.id || ''
        form.value.captcha_id = captchaImg.value.id
      }
    })
    .catch((err) => {
      console.error('获取验证码请求失败:', err)
      captchaImg.value.src = ''
      captchaImg.value.id = ''
      form.value.captcha_id = ''
    })
}

onMounted(() => {
  refreshCaptcha()
})
</script>

<style scoped>
.login-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #f0f4f8, #d9e2ec, #bcccdc, #9fa9ba);
  background-size: 400% 400%;
  animation: gradient 15s ease infinite;
}

.login-wrapper {
  background: white;
  border-radius: 12px;
  padding: 40px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 420px;
}

.login-wrapper:hover {
  transform: translateY(-5px);
  box-shadow: 0 25px 50px rgba(0, 0, 0, 0.15);
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 24px;
  font-weight: 600;
}

.login-header p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.login-form {
  width: 100%;
}

.form-item {
  margin-bottom: 20px;
}

.input-field {
  width: 100%;
  height: 40px;
  border-radius: 6px;
  transition: border-color 0.3s ease;
}

.input-field:hover {
  border-color: #667eea;
}

.input-field:focus {
  border-color: #667eea;
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

/* 验证码区域样式 */
.captcha-wrapper {
  display: flex;
  gap: 10px;
  align-items: center;
}

.captcha-input {
  flex: 1; /* 输入框占剩余空间 */
}

.captcha-image-container {
  width: 100px;
  height: 40px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  overflow: hidden;
  background-color: #f8f9fa;
  transition: border-color 0.3s ease;
}

.captcha-image-container:hover {
  border-color: #409eff;
}

.captcha-image {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.captcha-placeholder {
  font-size: 12px;
  color: #909399;
  text-align: center;
  width: 100%;
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.remember-checkbox {
  font-size: 14px;
  color: #666;
}

.forgot-link {
  font-size: 14px;
  color: #667eea;
  transition: color 0.3s ease;
}

.forgot-link:hover {
  color: #764ba2;
}

.login-button {
  width: 100%;
  height: 44px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.login-button:hover {
  transform: translateY(-1px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
  background: linear-gradient(135deg, #764ba2 0%, #667eea 100%);
}

.login-button:active {
  transform: translateY(0);
}
</style>
